package com.feiyuan.core.service.impl;

import com.feiyuan.core.mapper.AdminRoleMapper;
import com.feiyuan.core.pojo.entity.AdminRole;
import com.feiyuan.core.service.AdminRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdminRoleServiceImpl extends ServiceImpl<AdminRoleMapper, AdminRole> implements AdminRoleService {
    @Override
    public IPage<AdminRole> listPage(Integer pageNo, Integer pageSize, String roleName, String roleCode) {
        return this.baseMapper.selectPage(new Page<AdminRole>(pageNo, pageSize),
                new QueryWrapper<AdminRole>().like(StringUtils.isNotEmpty(roleName), "role_name", roleName).eq(StringUtils.isNotEmpty(roleCode), "role_code", roleCode));
    }

    @Override
    public boolean saveOrUpdateRole(AdminRole adminRole) {
        if (adminRole.getId() != null) {
            this.baseMapper.updateById(adminRole);
        } else {
            this.baseMapper.insert(adminRole);
        }
        return true;
    }

    @Override
    public List<String> selectRoles(Long userId) {
        List<AdminRole> adminRoles = this.baseMapper.selectRoles(userId);
        List<String> roles = new ArrayList<>();
        if (adminRoles != null && adminRoles.size() > 0) {
            for (AdminRole adminRole : adminRoles) {
                roles.add(adminRole.getRoleCode());
            }
        }
        return roles;
    }
}
